package jtech;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class Key implements KeyListener {

    private static final int NUM_KEY_CODES = 600;
    
    private boolean[] currentState = new boolean[NUM_KEY_CODES];
    private boolean[] nextState = new boolean[NUM_KEY_CODES];
    private int[] amount = new int[NUM_KEY_CODES];
    
    protected void update() {
        for (int i = 0; i < currentState.length; i++) {
            if (isReleased(i)) {
                amount[i] = 0;
            } if (isDown(i)) {
                amount[i]++;
            }
            currentState[i] = nextState[i];
        }
    }
    
    public void setNextState(int key, boolean value) {
        nextState[key] = value;
    }

    public int getAmount(int key) {
        return amount[key];
    }
    
    public boolean isDown(int key) {
        return currentState[key];
    }

    public boolean isPressed(int key) {
        return !currentState[key] && nextState[key];
    }

    public boolean isReleased(int key) {
        return currentState[key] && !nextState[key];
    }

    public void releaseAll() {
        for (int i = 0; i < nextState.length; i++) {
            nextState[i] = false;
        }
    }
    
    @Override
    public void keyPressed(KeyEvent e) {
        setNextState(e.getKeyCode(), true);
    }

    @Override
    public void keyReleased(KeyEvent e) {
        setNextState(e.getKeyCode(), false);
    }

	@Override
	public void keyTyped(KeyEvent e) {

	}

}
